<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="问诊时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="问诊时间"></el-input>
    </el-form-item>
    <el-form-item label="患者编号" prop="patientId">
      <el-input v-model="dataForm.patientId" placeholder="患者编号"></el-input>
    </el-form-item>
    <el-form-item label="问诊费用" prop="payMoney">
      <el-input v-model="dataForm.payMoney" placeholder="问诊费用"></el-input>
    </el-form-item>
    <el-form-item label="病情" prop="patientDisease">
      <el-input v-model="dataForm.patientDisease" placeholder="病情"></el-input>
    </el-form-item>
    <el-form-item label="医生ID" prop="doctorId">
      <el-input v-model="dataForm.doctorId" placeholder="医生ID"></el-input>
    </el-form-item>
    <el-form-item label="诊断结果" prop="diseaseResult">
      <el-input v-model="dataForm.diseaseResult" placeholder="诊断结果"></el-input>
    </el-form-item>
    <el-form-item label="病情描述" prop="diseaseDescribe">
      <el-input v-model="dataForm.diseaseDescribe" placeholder="病情描述"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          createTime: '',
          patientId: '',
          payMoney: '',
          patientDisease: '',
          doctorId: '',
          diseaseResult: '',
          diseaseDescribe: ''
        },
        dataRule: {
          createTime: [
            { required: true, message: '问诊时间不能为空', trigger: 'blur' }
          ],
          patientId: [
            { required: true, message: '患者编号不能为空', trigger: 'blur' }
          ],
          payMoney: [
            { required: true, message: '问诊费用不能为空', trigger: 'blur' }
          ],
          patientDisease: [
            { required: true, message: '病情不能为空', trigger: 'blur' }
          ],
          doctorId: [
            { required: true, message: '医生ID不能为空', trigger: 'blur' }
          ],
          diseaseResult: [
            { required: true, message: '诊断结果不能为空', trigger: 'blur' }
          ],
          diseaseDescribe: [
            { required: true, message: '病情描述不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/doctor/doctorpatientmid/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.createTime = data.doctorPatientMid.createTime
                this.dataForm.patientId = data.doctorPatientMid.patientId
                this.dataForm.payMoney = data.doctorPatientMid.payMoney
                this.dataForm.patientDisease = data.doctorPatientMid.patientDisease
                this.dataForm.doctorId = data.doctorPatientMid.doctorId
                this.dataForm.diseaseResult = data.doctorPatientMid.diseaseResult
                this.dataForm.diseaseDescribe = data.doctorPatientMid.diseaseDescribe
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/doctor/doctorpatientmid/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'createTime': this.dataForm.createTime,
                'patientId': this.dataForm.patientId,
                'payMoney': this.dataForm.payMoney,
                'patientDisease': this.dataForm.patientDisease,
                'doctorId': this.dataForm.doctorId,
                'diseaseResult': this.dataForm.diseaseResult,
                'diseaseDescribe': this.dataForm.diseaseDescribe
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
